/**
 * logmanager.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logmanager.h"
#include "logger.h"
#include <boost/foreach.hpp>

LogManager LogManager::m_instance;

///////////////////////////////////////////////////////////////
LogManager::LogManager() :
    m_loggers()
{
}

///////////////////////////////////////////////////////////////
LogManager::~LogManager()
{
    BOOST_FOREACH( const tLoggers::value_type & loggerPair, m_loggers )
    {
        Logger * logger = loggerPair.second;
        if( logger )
            delete logger;
    }

    m_loggers.clear();
}

///////////////////////////////////////////////////////////////
LogManager & LogManager::Instance()
{
    return m_instance;
}

///////////////////////////////////////////////////////////////
void LogManager::SubscribeFileLogger( tLogId a_id, const std::string & a_name, log4cpp::Priority::PriorityLevel a_priority )
{
    tLoggers::iterator it = m_loggers.find( a_id );
    if( it == m_loggers.end() )
        m_loggers[ a_id ] = new Logger( a_name, FILE_LOGGER, a_priority );
}

///////////////////////////////////////////////////////////////
void LogManager::SubscribeConsoleLogger( tLogId a_id, const std::string & a_name, log4cpp::Priority::PriorityLevel a_priority )
{
    tLoggers::iterator it = m_loggers.find( a_id );
    if( it == m_loggers.end() )
        m_loggers[ a_id ] = new Logger( a_name, COUT_LOGGER, a_priority );
}

///////////////////////////////////////////////////////////////
const Logger * LogManager::GetLogger( tLogId a_id ) const
{
    tLoggers::const_iterator it = m_loggers.find( a_id );
    if( it != m_loggers.end() )
        return it->second;
    return 0;
}

///////////////////////////////////////////////////////////////
Logger * LogManager::GetLogger( tLogId a_id )
{
    tLoggers::iterator it = m_loggers.find( a_id );
    if( it != m_loggers.end() )
        return m_loggers[ a_id ];
    return 0;
}

///////////////////////////////////////////////////////////////
void LogManager::Log( tLogId a_id, const std::string & a_logMsg, LogLevel a_level )
{
    Logger * logger = GetLogger( a_id );
    if( logger )
    {
        switch( a_level )
        {
        case ERROR_LOG: logger->LogError() << a_logMsg; break;
        case WARN_LOG:  logger->LogWarn() << a_logMsg; break;
        case DEBUG_LOG: logger->LogDebug() << a_logMsg; break;
        case INFO_LOG:
        case N_LOG_LEVELS:
        default:
            logger->LogInfo() << a_logMsg; break;
        }
    }
}
